//
//  XHMoreViewController.m
//  Big Show
//
//  Created by 赵晓航 on 15-5-13.
//  Copyright (c) 2015年 Big_Q. All rights reserved.
//

#import "XHMoreViewController.h"
#import "WXMoreTableViewCell.h"
#import "WXLogInViewController.h"
#import "XHMoreTableViewCell.h"

#define WIDTH [UIScreen mainScreen].bounds.size.width / 375
#define HEIGHT [UIScreen mainScreen].bounds.size.height / 667
#define kHeight self.view.frame.size.height / 2.5

@interface XHMoreViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) UIButton *buttonHC;
@property (nonatomic, retain) UILabel *label;
@property(nonatomic, retain) UITableView *tableView;
@property(nonatomic, retain) NSString *huancun;
@property(nonatomic, retain) NSIndexPath *myindex;

@end

@implementation XHMoreViewController


- (void)dealloc
{
    [_myindex release];
    [_huancun release];
    [_label release];
    [_buttonHC release];
    [_tableView release];
    [super dealloc];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"用户";
    self.navigationController.navigationBar.translucent = NO;
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self createTableView];
    [self createImageView];
    
    


}



- (void)createImageView
{

    UIImage *image = [UIImage imageNamed:@"登陆图"];

    self.userImageView = [[UIImageView alloc] initWithImage:image];
    self.userImageView.frame = CGRectMake(0, -kHeight, self.view.frame.size.width, kHeight);
    UITapGestureRecognizer *gesture1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    gesture1.numberOfTapsRequired = 2;
    self.userImageView.userInteractionEnabled = YES;
    [self.userImageView addGestureRecognizer:gesture1];
    [self.tableView addSubview:self.userImageView];
}

- (void)createTableView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 113)];
    _tableView.dataSource = self;
    _tableView.delegate = self;
//    _tableView.tableHeaderView = _userImageView;
    [self.view addSubview:_tableView];
    [_tableView release];
    _tableView.contentInset = UIEdgeInsetsMake(kHeight, 0, 0, 0);
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 4;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    WXMoreTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[WXMoreTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
    }

    if (indexPath.row == 1) {
        WXMoreTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
        if (!cell) {
            cell = [[WXMoreTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
        }
        cell.textLabel.text = @"夜间模式";
        cell.textLabel.font = [UIFont fontWithName:@"Courier-BoldOblique" size:17];
        [cell.swich addTarget:self action:@selector(night:) forControlEvents:UIControlEventValueChanged];
        return cell;

    } else if (indexPath.row == 2) {
        XHMoreTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"celll"];
        if (!cell) {
            cell = [[XHMoreTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"celll"];
        }
        
        cell.textLabel.text = @"清除缓存";
        cell.textLabel.font = [UIFont fontWithName:@"Courier-BoldOblique" size:17];
        cell.label.text = self.huancun;
        self.myindex = indexPath;
        
        return cell;
    } else {
        
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell1"];
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell1"];
            
        }
        
        if (indexPath.row == 0) {
            cell.textLabel.text = @"登   录";
            cell.textLabel.font = [UIFont fontWithName:@"Courier-BoldOblique" size:17];
            
        } else if (indexPath.row == 3) {
            cell.textLabel.text = @"关于我们";
            cell.textLabel.font = [UIFont fontWithName:@"Courier-BoldOblique" size:17];
        }
        return cell;
    }
}


-(void) night:(UISwitch *) sw
{
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    
    NSString *f1 = [NSString stringWithFormat:@"%f", 0.5];
    
    NSString *f2 = [NSString stringWithFormat:@"%f", 1.0];

    
    if (sw.isOn) {
        [center postNotificationName:@"ah" object:f1];
    }else{
        [center postNotificationName:@"ah" object:f2];
    }
    
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.row == 0) {
        WXLogInViewController *longin = [[WXLogInViewController alloc] init];
        [self.navigationController pushViewController:longin animated:YES];
    }
    if (indexPath.row == 2) {
        [self testAction];
    }
    if (indexPath.row == 3) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Big Show" message:@"开发者:赵晓航 王希 高君树\n 邮箱:13149505150@163.com" delegate:nil cancelButtonTitle:@"谢谢支持" otherButtonTitles: nil];
        [alert show];
        [alert release];
    }
    
}



-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    
    CGFloat f = scrollView.contentOffset.y + kHeight;
//    NSLog(@"%f", f);
    self.userImageView.frame = CGRectMake(0, -kHeight + f, self.view.frame.size.width, kHeight - f);
    
}




-(void)login
{
    WXLogInViewController *longin = [[WXLogInViewController alloc] init];
    [self.navigationController pushViewController:longin animated:YES];
}




// 1.点击图片触发的方法
-(void)tapAction:(UITapGestureRecognizer *)sender
{
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
    imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    imagePicker.delegate = self;
    imagePicker.allowsEditing = YES;
    
    [self presentViewController:imagePicker animated:YES completion:^{
        
        
    }];
}


-(void)viewDidAppear:(BOOL)animated
{
    [self huancunAction];
//    [_tableView reloadData];
}

// 2.获取选中的照片 显示并赋值给view的属性userImageView
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    self.userImageView.image = [info valueForKey:@"UIImagePickerControllerOriginalImage"];
    // 通过模态返回
    [picker dismissViewControllerAnimated:YES completion:^{
        
    }];
}

// 3.最后选中结束后 通过模态消失
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [picker dismissViewControllerAnimated:YES completion:^{
        
    }];
}



//清除缓存
- (void)testAction
{
    NSString * cachPath = [ NSSearchPathForDirectoriesInDomains ( NSCachesDirectory , NSUserDomainMask , YES ) firstObject ];
    
    NSArray * files = [[ NSFileManager defaultManager ] subpathsAtPath :cachPath];
    
    // NSLog ( @"cachpath = %@" , cachPath);
    
    for ( NSString * p in files) {
        
        NSError * error = nil ;
        
        NSString * path = [cachPath stringByAppendingPathComponent :p];
        
        if ([[ NSFileManager defaultManager ] fileExistsAtPath :path]) {
            
            [[ NSFileManager defaultManager ] removeItemAtPath :path error :&error];
            
        }
    }
    NSString *romNumber = [NSString stringWithFormat:@"%.2fM", [self filePath]];

    XHMoreTableViewCell *cell = (XHMoreTableViewCell *)[_tableView cellForRowAtIndexPath:self.myindex];
    cell.label.text = romNumber;
    
    
    
//    self.huancun = romNumber;
//    [_tableView reloadData];
}


- (void)huancunAction {
    NSString *chch = [ NSString stringWithFormat : @"%.2fM" , [ self filePath ]];
    NSLog(@"++++____++++_+_+_+_+     我的缓存   %@", chch);
    NSString *romNumber = [NSString stringWithFormat:@"%.2fM", [self filePath]];
    
    
    XHMoreTableViewCell *cell = (XHMoreTableViewCell *)[_tableView cellForRowAtIndexPath:self.myindex];
    cell.label.text = romNumber;
    
//    [_buttonHC setTitle:romNumber forState:UIControlStateNormal];
    self.huancun = romNumber;
//    [_buttonHC setTitleColor:[UIColor colorWithRed:0.478 green:0.651 blue:0.988 alpha:1.0] forState:UIControlStateNormal];
    
}

- (UIColor *)loadTitleViewColor {
    return [UIColor colorWithRed:0.618 green:0.618 blue:0.618 alpha:1];
}



//-------------获取缓存
- ( float )filePath {
    NSString * cachPath = [ NSSearchPathForDirectoriesInDomains ( NSCachesDirectory , NSUserDomainMask , YES ) firstObject ];
    return [ self folderSizeAtPath :cachPath];
}
- ( float ) folderSizeAtPath:( NSString *) folderPath {
    NSFileManager * manager = [ NSFileManager defaultManager ];
    if (![manager fileExistsAtPath :folderPath]) return 0 ;
    NSEnumerator *childFilesEnumerator = [[manager subpathsAtPath :folderPath] objectEnumerator ];
    NSString * fileName;
    long long folderSize = 0 ;
    while ((fileName = [childFilesEnumerator nextObject ]) != nil ){
        NSString * fileAbsolutePath = [folderPath stringByAppendingPathComponent :fileName];
        
        folderSize += [ self fileSizeAtPath :fileAbsolutePath];
        
    }
    
    return folderSize/( 1024.0 * 1024.0 );
    
}
- (long long) fileSizeAtPath:( NSString *) filePath{
    
    NSFileManager * manager = [ NSFileManager defaultManager ];
    
    if ([manager fileExistsAtPath :filePath]){
        
        return [[manager attributesOfItemAtPath :filePath error : nil ] fileSize ];
    }
    return 0 ;
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
